package com.plm.mqshequ.nosql;

import com.mongodb.client.result.UpdateResult;
import com.plm.mqshequ.entity.PageModel;
import com.plm.mqshequ.entity.Post;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author Limi
 * @create 2019-01-16 14:08
 */
@Repository
public class MongoPostOps {

    @Autowired
    private MongoTemplate template;

    /**
     * 创建帖子
     */
    public void save (Post post) {
        template.save(post);
    }

    /**
     * 根据post id查询帖子
     */
    public Post findById(String id){
        Query query = new Query(
                Criteria.where("_id").is(id)
                .and("status").is("0")
        );
        return template.findOne(query, Post.class);
    }

    /**
     * 根据主题查询帖子
     */
    public List<Post> findByTopic(String topic){
        Query query = new Query(
                Criteria.where("topic").is(topic)
                        .and("status").is("0")
        );
        return template.find(query, Post.class);
    }

    /**
     * 根据用户Id查询帖子
     */
    public List<Post> findByUid(String uid){
        Query query = new Query(
                Criteria.where("uid").is(uid)
                        .and("status").is("0")
        );
        return template.find(query, Post.class);
    }

    /**
     * 查询最近10条提问
     * @param uid
     * @return
     */
    public List<Post> findByUidRecent(String uid){
        Query query = new Query(
                Criteria.where("uid").is(uid)
                        .and("status").is("0")
        );
        //按创建时间降序排序
        Sort sort = new Sort(Sort.Direction.DESC, "createTime");
        query.with(sort);
        //设置起始数
        query.skip(0);
        //设置查询条数
        query.limit(10);
        return template.find(query, Post.class);
    }

    /**
     * 查询所有正常状态的帖子
     */
    public List<Post> findByAll(){
        Query query = new Query(Criteria.where("status").is("0"));
        return template.find(query, Post.class);
    }

    /**
     * 分页查询数据根据条件查询
     */
    public PageModel findByPage(PageModel pageModel, String column, String filter){
        String topic = null;
        Query query = null;
        if (!"main".equals(column)) {
            topic = column;
        }
        if (null != topic) {//
            if ("0".equals(filter)) {// 综合
                query = new Query(Criteria.where("status").is("0").and("topic").is(topic));
            } else if ("1".equals(filter)) {//未结
                query = new Query(Criteria.where("status").is("0").and("topic").is(topic).and("isEnd").is("1"));
            } else if ("2".equals(filter)) {//已结
                query = new Query(Criteria.where("status").is("0").and("topic").is(topic).and("isEnd").is("0"));
            } else {//精华
                query = new Query(Criteria.where("status").is("0").and("topic").is(topic).and("numReply").gt(30));
            }
        } else {
            if ("0".equals(filter)) {// 综合
                query = new Query(Criteria.where("status").is("0") );
            } else if ("1".equals(filter)) {//未结
                query = new Query(Criteria.where("status").is("0").and("isEnd").is("1"));
            } else if ("2".equals(filter)) {//已结
                query = new Query(Criteria.where("status").is("0").and("isEnd").is("0"));
            } else {//精华
                query = new Query(Criteria.where("status").is("0").and("numReply").gt(30));
            }
        }

        //按创建时间降序排序
        Sort sort = new Sort(Sort.Direction.DESC, "createTime");
        query.with(sort);
        //设置起始数
        query.skip((pageModel.getCurr() - 1) * pageModel.getLimit());
        //设置查询条数
        query.limit(pageModel.getLimit());
        List<Post> list = template.find(query, Post.class);

        int total = (int) template.count(query, Post.class);
        int size = pageModel.getLimit();
        int totalPage = total % size == 0 ? total / size : (total / size) + 1;

        pageModel.setTotal(total);
        pageModel.setPages(totalPage);
        pageModel.setList(list);

        return pageModel;
    }

    /**
     * 分页查询数据
     */
    public PageModel findByPage(PageModel pageModel){
        Query query = new Query(Criteria.where("status").is("0"));
        //按创建时间降序排序
        Sort sort = new Sort(Sort.Direction.DESC, "createTime");
        query.with(sort);
        //设置起始数
        query.skip((pageModel.getCurr() - 1) * pageModel.getCurr());
        //设置查询条数
        query.limit(pageModel.getLimit());
        List<Post> list = template.find(query, Post.class);

        int total = (int) template.count(query, Post.class);
        int size = pageModel.getLimit();
        int totalPage = total % size == 0 ? total / size : (total / size) + 1;

        pageModel.setTotal(total);
        pageModel.setPages(totalPage);
        pageModel.setList(list);

        return pageModel;
    }

    /**
     * 更改帖子
     */
    public void update (Post post) {
        Query query = new Query(Criteria.where("_id").is(post.get_id()));
        Update update = new Update();
        update.set("topic", post.getTopic())
              .set("treasure", post.getTreasure())
              .set("title", post.getTitle())
              .set("content", post.getContent());
        template.updateFirst(query, update, Post.class);
    }

    /**
     * 更新所有数据
     * @param post
     */
    public void updateAll (Post post) {
        Query query = new Query(Criteria.where("_id").is(post.get_id()));
        Update update = new Update();
        update.set("topic", post.getTopic())
                .set("treasure", post.getTreasure())
                .set("title", post.getTitle())
                .set("content", post.getContent())
                .set("reply", post.getReply())
                .set("tag", post.getTag())
                .set("numLook", post.getNumLook())
                .set("numLook", post.getNumReply())
                .set("status", post.getStatus())
                .set("isEnd", post.getIsEnd());
        template.updateFirst(query, update, Post.class);
    }

    /**
     * 更新评论数
     * @param post
     */
    public void updateNumReply(Post post) {
        Query query = new Query(Criteria.where("_id").is(post.get_id()));
        Update update = new Update();
        update.set("numReply", post.getNumReply());
        template.updateFirst(query, update, Post.class);
    }

    /**
     * 更新查看数
     * @param post
     */
    public void updateNumLook(Post post) {
        Query query = new Query(Criteria.where("_id").is(post.get_id()));
        Update update = new Update();
        update.set("numLook", post.getNumLook());
        template.updateFirst(query, update, Post.class);
    }

    /**
     * 删除帖子
     */
    public void reomve (String id) {
        Query query = new Query(Criteria.where("id").is(id));
        template.remove(query, Post.class);
    }

}
